/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.schedule.core.service.processor.active;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.schedule.core.domain.ScheduleJob;
import info.peigen.hotpot.component.schedule.core.service.processor.AbstractScheduleServiceProcessor;
import info.peigen.hotpot.component.schedule.facade.command.order.ScheduleJobEditOrder;
import org.noear.solon.annotation.Component;

/**
 * <b>(ScheduleJobActiveProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/7
 */
@Component
public class ScheduleJobActiveProcessor extends AbstractScheduleServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 10;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        Integer     id          = switchId(serviceContext);
        ScheduleJob scheduleJob = scheduleJobRepository.active(id);
        Assert.notNull(scheduleJob, () -> new BusinessException(ResultCode.FAILURE, "未能激活scheduleJob[Id=" + id + "]"));
        scheduleJob.check();
        serviceContext.setDomain(scheduleJob);
    }

    private Integer switchId(ProcessorServiceContext serviceContext) {
        ServiceOrderBase order = serviceContext.getOrder();
        if (order instanceof ScheduleJobEditOrder) {
            return ((ScheduleJobEditOrder) order).getId();
        } else {
            return null;
        }
    }
}